package bar.dark.lee.common;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class BDAddWidget{
	
	/**
	 * 데이터 갯수에 따른 레이아웃 생성
	 * row, column 수에 따른 위젯 생성
	 * 
	 * 클래스화 중임.........................
	 * 
	 */
	private Context mContext;
	private int mLinearId = 101;
	private int mCategoryBtnId = 201;
	
	public BDAddWidget(Context context) {
		mContext = context;
	}
	
	/**
	 * 데이터 갯수에 따라 레이아웃 추가
	 * @param size 행 또는 열 갯수
	 * @param parentLinear 부모 리니어
	 * @param params 레이아웃 파라미터
	 */
	public void addLinear(int size, LinearLayout parentLinear, LinearLayout.LayoutParams params, int orientation) {
		Log.e("<< BDAddWidget >>", "  addLinear("+size+", "+params+")");
		for (int i=0; i<size; i++) {
			LinearLayout linear = new LinearLayout(mContext);
			linear.setId(mLinearId+i);
			linear.setLayoutParams(params);
			linear.setOrientation(orientation);
			parentLinear.addView(linear);
			Log.d("<< BDAddWidget >>", "    parentLinear.addView("+linear.getId()+")");
		}
	}
	
	/**
	 * data갯수에 따라 레이아웃 추가하고 
	 * weight에 따라 위젯 추가한다.
	 * @param array data list
	 * @param parentLinear 부모linear
	 * @param maxRowSize 최대 행수
	 * @param maxColumnSize 최대 열수
	 * @param weight 위젯 가중치
	 */
	public void addWidgetWeight(ArrayList<String> array, LinearLayout parentLinear, 
			int maxRowSize, int maxColumnSize, int weight) {
		Log.e("<< BDAddWidget >>", "  addWidget()");
		for (int i=0; i<array.size(); i++) {
			Log.d("<< BDAddWidget >>", "    array ::: "+i+":"+array.get(i));
		}
		LinearLayout.LayoutParams paramsFF = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		LinearLayout.LayoutParams paramsF0 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, weight);
		LinearLayout.LayoutParams params0F = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT, weight);
		LinearLayout.LayoutParams widgetParams = null;
		int orientation = parentLinear.getOrientation();	// horizontal : 0, vertical = 1
		
		parentLinear.removeAllViews();
		
		int buttonSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, mContext.getResources().getDisplayMetrics());
		
//		if (orientation == 0) {
//			for (int columnSize=1; columnSize<=maxColumnSize; columnSize++) {
//				if ((columnSize-1)*maxRowSize < array.size()  
//						&& array.size() <= columnSize*maxRowSize) {
//					if (columnSize == 1) {
//						addLinear(columnSize, parentLinear, paramsFF, 1);
//					} else {
//						addLinear(columnSize, parentLinear, params0F, 1);
//					}
//				}
//				if (maxRowSize*maxColumnSize < array.size()){
//					addLinear(maxRowSize, parentLinear, params0F, 1);
//					break;
//				}
//			}
//			widgetParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, buttonSize);
//		} else 
		if (orientation == 1) {
			for (int rowSize=1; rowSize<=maxRowSize; rowSize++) {
				if ((rowSize-1)*maxColumnSize < array.size()  
						&& array.size() <= rowSize*maxColumnSize) {
					if (rowSize == 1) {
						addLinear(rowSize, parentLinear, paramsFF, 0);
						break;
					} else {
						addLinear(rowSize, parentLinear, paramsF0, 0);
						break;
					}
				}
				if (maxRowSize*maxColumnSize < array.size()){
					addLinear(maxRowSize, parentLinear, paramsF0, 0);
					break;
				}
			}
			widgetParams = new LinearLayout.LayoutParams(buttonSize, LayoutParams.FILL_PARENT);
		}
		
		LinearLayout linear = new LinearLayout(mContext);
		for (int size=0; size<array.size(); size++) {
			//LinearLayout linear = new LinearLayout(mContext);
			Button btn = new Button(mContext);
			btn.setText(array.get(size));
			
			//btn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
			btn.setGravity(Gravity.CENTER);
			btn.setId(mCategoryBtnId++);
			btn.setLayoutParams(widgetParams);
			
			for (int j=1; j<=parentLinear.getChildCount(); j++) {
				if (size < j*maxColumnSize) {
					linear = ((LinearLayout)parentLinear.getChildAt(j-1));
					linear.addView(btn);
					break;
				}
			}
			Log.d("<< BDAddWidget >>", "    "+linear.getId()+".addView("+btn.getId()+")");
		}
		Button btn2 = new Button(mContext);
		btn2.setText("설정");
		btn2.setGravity(Gravity.CENTER);
		btn2.setId(mCategoryBtnId++);
		btn2.setLayoutParams(widgetParams);
		linear.addView(btn2);
	}
	
	/**
	 * 나중에 할래
	 */
	public void addWidgetWidthHeight() {
	
	}
	
	/**
	 * 남은 공간에 빈뷰 생성
	 * @param parentLinear 부모linear
	 * @param columnSize 최대 열수
	 */
	public void addEmptyView(LinearLayout parentLinear, int maxColumnSize) {
		Log.e("<< BDAddWidget >>", "  addEmptyView()");
		int count = parentLinear.getChildCount();
		LinearLayout linear = (LinearLayout)parentLinear.getChildAt(count-1);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT, 1);
		count = maxColumnSize - linear.getChildCount();
		View view = new View(mContext);
		view.setLayoutParams(params);
		for (int i=0; i<count; i++) {
			linear.addView(view);
			Log.d("<< BDAddWidget >>", "    "+linear.getId()+".addView(emptyView)");
		}
	}
}
